package com.lixw.aiassistant.handler;

import com.lixw.aiassistant.service.MemoryService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @ClassName: AbstractChainHandler
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/13 23:15
 **/
@Slf4j
public abstract class AbstractChainHandler implements ChatChainHandler{
    @Resource
    private MemoryService memoryService;
    @Resource
    protected ChatMemory chatMemory;
    @Resource
    protected ChatClient chatClient;

    @Override
    public Flux<String> handle(String sessionId, String userMessage) {
        //获取用户的历史记录
        List<Message> conversationContext = memoryService.getConversationContext(sessionId);
        //格式化历史记录
        String formatChatHistory = memoryService.formatChatHistory(conversationContext, userMessage);
        //对话增强
        String enhancedPrompt = memoryService.getEnhancedPrompt(sessionId, formatChatHistory);
        //处理消息
        return this.doHandle(sessionId, enhancedPrompt);
    }

    protected abstract Flux<String> doHandle(String sessionId, String enhancedPrompt);
}